package cn.com.lms.business.exam.service;

import cn.com.lms.app.api.exam.vo.ExerciseQuestionSubjectVO;
import cn.com.lms.app.api.exam.vo.ExerciseSubjectVO;
import cn.com.lms.business.core.constant.ErrorEnum;
import cn.com.lms.business.core.util.SystemUserUtil;
import cn.com.lms.business.exam.enums.SubjectTypeEnum;
import cn.com.lms.business.exam.utils.AsscilCodeUtil;
import cn.com.lms.business.exam.utils.JsonUtil;
import cn.com.lms.business.exam.web.vo.QuestionSubjectExportVO;
import cn.com.lms.business.exam.web.vo.QuestionSubjectVO;
import cn.com.lms.business.excel.ExcelService;
import cn.com.lms.business.excel.ExcelSheetDataDTO;
import cn.com.lms.business.exam.persistence.entity.*;
import cn.com.lms.business.exam.persistence.repository.QuestionSubjectRepository;
import cn.com.lms.business.excel.ExportThread;
import cn.com.lms.business.user.persistence.entity.SysUserVo;
import com.alibaba.fastjson.JSON;

import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.export.Column;
import com.bnzj.core.export.ExportUtils;

import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;

import com.bnzj.core.util.BeanConvertUtils;

import com.bnzj.core.util.BeanUtils;
import com.bnzj.core.util.DateUtils;
import com.bnzj.core.util.FbConstant;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.hibernate.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class QuestionSubjectService extends BaseService<QuestionSubjectRepository, QuestionSubject, Long> {

    @Autowired
    QuestionSubjectFileService questionSubjectFileService;

    @Autowired
    QuestionSubjectOptionService questionSubjectOptionService;


    @Autowired
    QuestionSubjectFillService questionSubjectFillService;


    @Autowired
    QuestionSubjectAnswerService questionSubjectAnswerService;

    @Autowired
    ExcelService excelService;

//    private static final List<Column> columns = new ArrayList<Column>();
//
//    static {
//        columns.add(new Column("subject", "题目", 0));
//        columns.add(new Column("parse", "答案解析", 1));
//        columns.add(new Column("answer", "正确答案", 2));
//        columns.sort(null);
//
//    }
//
//    public static List<Column> getColumnList() {
//        return Collections.unmodifiableList(columns);
//    }

    @Override
    public Optional<QuestionSubject> findById(Long id) {

        Optional<QuestionSubject> questionSubjectOpt = super.findById(id);
        if (!questionSubjectOpt.isPresent()) {
            throw new BusinessException(ErrorEnum.RECORD_NOT_EXIST.getMessage(), "" + ErrorEnum.RECORD_NOT_EXIST.getCode());
        }

        //文件
        List<QuestionSubjectFile> questionSubjectFileList = questionSubjectFileService.findQuestionSubjectBySubjectId(id);
        questionSubjectOpt.get().setQuestionSubjectFileList(questionSubjectFileList);

        if (SubjectTypeEnum.SINGLE_OPTION.getValue().equals(questionSubjectOpt.get().getType()) || SubjectTypeEnum.MULTIPLE_OPTION.getValue().equals(questionSubjectOpt.get().getType())) {
            //根据试题ID,查询选择题选项列表
            List<QuestionSubjectOption> questionSubjectOptionList = questionSubjectOptionService.findQuestionSubjectOptionBySubjectId(id);
            questionSubjectOpt.get().setQuestionSubjectOptionList(questionSubjectOptionList);
        }
        if (SubjectTypeEnum.JUDGE.getValue().equals(questionSubjectOpt.get().getType())) {
            //根据试题ID,查询判断题答案
            Optional<QuestionSubjectAnswer> answerOpt = questionSubjectAnswerService.findBySubjectId(questionSubjectOpt.get().getId());
            if (answerOpt.isPresent()) {
                questionSubjectOpt.get().setIsRight(answerOpt.get().getAnswer().equals("正确") || answerOpt.get().getAnswer().equals("true"));
            }

        }

        if (SubjectTypeEnum.FILL.getValue().equals(questionSubjectOpt.get().getType())) {
            //根据试题ID,查询填空题答案
            QuestionSubjectFill questionSubjectFill = new QuestionSubjectFill();
            questionSubjectFill.setSubjectId(id);
            questionSubjectFill.setIsDelete(false);
            List<QuestionSubjectFill> questionSubjectFillList = questionSubjectFillService.findAll(questionSubjectFill);
            questionSubjectFillList = questionSubjectFillList.stream().map(m -> {
                m.setAnswerList(JSON.parseArray(m.getAnswer(), String.class));
                return m;
            }).collect(Collectors.toList());


            questionSubjectOpt.get().setQuestionSubjectFillList(questionSubjectFillList);
        }
        Optional<QuestionSubjectAnswer> answerOpt = questionSubjectAnswerService.findBySubjectId(questionSubjectOpt.get().getId());
        if (answerOpt.isPresent()) {
            questionSubjectOpt.get().setAnswer(answerOpt.get().getAnswer());
            questionSubjectOpt.get().setParse(answerOpt.get().getParse());
        }

        return questionSubjectOpt;

    }


    public static QueryHelpBean getQueryHelpBean() {
        String selectSQL = "select qs.id,qs.question_id,qs.type,qs.subject,qs.correct_status,qs.is_delete,qs.create_time,qs.creator_name,q.question_name,sa.answer,sa.parse," +
                "(select group_concat(CONCAT(option_name,\" \",content) SEPARATOR '###')   from bnz_exam_question_subject_option so  where so.subject_id=qs.id GROUP BY subject_id) options, "+
                " (select group_concat(CONCAT(file_path))   from bnz_exam_question_subject_file sf where sf.subject_id=qs.id " +
                " GROUP BY subject_id) images";
        String whereSQL = " from bnz_exam_question_subject qs left join bnz_exam_question q on qs.question_id=q.id " +
                " left join bnz_exam_question_subject_answer sa on sa.subject_id=qs.id  where 1=1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, QuestionSubjectVO.class);
        return queryBean;
    }

    public static QueryHelpBean getExciseSubjectQueryHelpBean(Long exerciseId) {
        String selectSQL = "select  qs.id as subject_id,qs.question_id,qs.type,qs.subject,sa.answer,sa.parse," +
                " (select group_concat(CONCAT(option_name,\" \",content) SEPARATOR '###')   from bnz_exam_question_subject_option so  where so.subject_id=qs.id GROUP BY subject_id) options, " +
                " (select group_concat(CONCAT(file_path))   from bnz_exam_question_subject_file sf where sf.subject_id=qs.id " +
                " GROUP BY subject_id) images";
        String whereSQL = " from bnz_exam_question_subject qs left join bnz_exam_question_subject_answer sa on sa.subject_id=qs.id where qs.question_id in (select eq.question_id from bnz_exam_exercise_question eq  where eq.exercise_id=" + exerciseId + ")  ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, ExerciseSubjectVO.class);
        return queryBean;
    }



    public  List<Map<String,Object>> findExerciseQuestionSubjectVO(Long exerciseId){

        return  repository.selectQuestionSubjectByExerciseId(exerciseId);
    }


    @Transactional(rollbackFor = Exception.class)
    public QuestionSubject addQuestionSubject(QuestionSubject questionSubject) {
        // 保存试题信息
        QuestionSubject result = save(questionSubject);
        result.setParse(questionSubject.getParse());
        StringBuffer answer = new StringBuffer();

        //保存试题图片信息
        questionSubjectFileService.batchSave(result, questionSubject.getQuestionSubjectFileList());

        //保存选择题信息
        String strAnswer = questionSubjectOptionService.batchSave(result, questionSubject.getQuestionSubjectOptionList());
        if (StringHelper.isNotEmpty(strAnswer)) {
            answer.append(strAnswer);
        }

        // 判断题答案
        if (SubjectTypeEnum.JUDGE.getValue().equals(result.getType()) && questionSubject.getIsRight() != null) {

            answer.append(questionSubject.getIsRight() ? "正确" : "错误");
        }

        // 保存填空题
        questionSubjectFillService.batchSave(result, questionSubject.getQuestionSubjectFillList());

        // 问答题答案
        if (SubjectTypeEnum.EXPLAIN.getValue().equals(result.getType()) && StringHelper.isNotEmpty(questionSubject.getAnswer())) {
            answer.append(questionSubject.getAnswer());
        }

        // 保存题目答案
        questionSubjectAnswerService.saveOrUpdate(result, answer.toString());

        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public QuestionSubject updateQuestionSubject(QuestionSubject questionSubject) {
        Optional<QuestionSubject> questionSubjectOpt = findById(questionSubject.getId());
        if (!questionSubjectOpt.isPresent()) {
            throw new BusinessException(ErrorEnum.RECORD_NOT_EXIST.getMessage(), "" + ErrorEnum.RECORD_NOT_EXIST.getCode());
        }
        QuestionSubject target = questionSubjectOpt.get();
        BeanUtils.copyProperties(questionSubject, target);
        save(target);

        List<QuestionSubjectFile> questionSubjectFileList = questionSubject.getQuestionSubjectFileList();
        if (!CollectionUtils.isEmpty(questionSubjectFileList)) {
            questionSubjectFileService.deleteBySubjectId(target.getId());
            questionSubjectFileService.batchSave(target, questionSubjectFileList);
        }else{
            questionSubjectFileService.deleteBySubjectId(target.getId());
        }

        List<QuestionSubjectOption> questionSubjectOptionList = questionSubject.getQuestionSubjectOptionList();
        if ((SubjectTypeEnum.SINGLE_OPTION.getValue().equals(questionSubject.getType()) || SubjectTypeEnum.MULTIPLE_OPTION.getValue().equals(questionSubject.getType())) && !CollectionUtils.isEmpty(questionSubjectOptionList)) {
            questionSubjectOptionService.deleteBySubjectId(target.getId());
            String answer = questionSubjectOptionService.batchSave(target, questionSubjectOptionList);
            questionSubjectAnswerService.saveOrUpdate(target, answer);
        }

        // 判断题答案
        if (SubjectTypeEnum.JUDGE.getValue().equals(target.getType()) && questionSubject.getIsRight() != null) {
            questionSubjectAnswerService.saveOrUpdate(target, questionSubject.getIsRight() != null && questionSubject.getIsRight() ? "正确" : "错误");

        }


        return target;
    }

    public List<ExcelSheetDataDTO> importExcelByInutStream(InputStream inputStream, Long questionId) {
        SysUserVo sysUser = SystemUserUtil.getCurrentUser();
        List<ExcelSheetDataDTO> excelSheetDataDTOList = new CopyOnWriteArrayList<>();
        try {
            excelSheetDataDTOList = excelService.readByInputStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 保存数据
        excelSheetDataDTOList.stream().forEach(excelSheetDataDTO -> {
            if (excelSheetDataDTO.getSheetName().equals("单选题") || excelSheetDataDTO.getSheetName().equals("多选题")) {
                List<List<Object>> allRows = excelSheetDataDTO.getAllRows();
                allRows.stream().forEach(row -> {
                    if (allRows.indexOf(row) == 0) {
                        return;
                    } else if (row.size() < 3) {
                        return;
                    } else {

                        String subject = row.get(0).toString();
                        String parse =StringUtils.isEmpty(row.get(1).toString())?"":row.get(1).toString() ;
                        String answer = row.get(2).toString();
                        QuestionSubject optionSubject = new QuestionSubject();
                        optionSubject.setSubject(subject);
                        optionSubject.setParse(parse);
                        optionSubject.setIsDelete(false);
                        optionSubject.setModifyTime(new Date());
                        optionSubject.setCreator(sysUser!=null?sysUser.getCreator():"1000000L");
                        optionSubject.setCreateTime(new Date());
                        optionSubject.setCreatorName(sysUser!=null?sysUser.getCname():"admin");
                        optionSubject.setQuestionId(questionId);
                        optionSubject.setType(excelSheetDataDTO.getSheetName().equals("单选题") ? SubjectTypeEnum.SINGLE_OPTION.getValue() : SubjectTypeEnum.MULTIPLE_OPTION.getValue());
                        QuestionSubject result = save(optionSubject);
                        List<QuestionSubjectOption> questionSubjectOptionList = new CopyOnWriteArrayList<>();
                        row.stream().forEach(column -> {
                            if (row.indexOf(column) == 0) {
                                return;
                            } else if (row.indexOf(column) == 1) {
                                return;
                            } else if (row.indexOf(column) == 2) {
                                return;
                            } else {
                                String title = allRows.get(0).get(row.indexOf(column)).toString();
                                String optionName = title.replace("选项", "");

                                if (!StringUtils.isEmpty(column)) {
                                    QuestionSubjectOption questionSubjectOption = new QuestionSubjectOption();
                                    questionSubjectOption.setOptionName(optionName);
                                    questionSubjectOption.setContent(column.toString());
                                    questionSubjectOption.setIsRight(answer.contains(optionName));
                                    questionSubjectOption.setSubjectId(result.getId());
                                    questionSubjectOption.setCreator(result.getCreator());
                                    questionSubjectOption.setCreateTime(result.getCreateTime());
                                    questionSubjectOption.setCreatorName(result.getCreatorName());
                                    questionSubjectOption.setIsDelete(false);
                                    questionSubjectOptionList.add(questionSubjectOption);
                                }
                            }
                        });
                        questionSubjectOptionService.saveAll(questionSubjectOptionList);
                        QuestionSubjectAnswer optionSubjectAnswer = new QuestionSubjectAnswer();
                        optionSubjectAnswer.setAnswer(answer);
                        optionSubjectAnswer.setSubjectId(result.getId());
                        optionSubjectAnswer.setCreator(result.getCreator());
                        optionSubjectAnswer.setCreateTime(result.getCreateTime());
                        optionSubjectAnswer.setCreatorName(result.getCreatorName());
                        optionSubjectAnswer.setParse(parse);
                        questionSubjectAnswerService.save(optionSubjectAnswer);
                    }

                });

            }

            if (excelSheetDataDTO.getSheetName().equals("判断题")) {
                List<List<Object>> allRows = excelSheetDataDTO.getAllRows();
                allRows.stream().forEach(row -> {
                    if (allRows.indexOf(row) == 0) {
                        return;
                    } else if (row.size() < 3) {
                        return;
                    } else {
                        String subject = row.get(0).toString();
                        String parse = row.get(1).toString();
                        String answer = row.get(2).toString();

                        QuestionSubject optionSubject = new QuestionSubject();
                        optionSubject.setSubject(subject);
                        optionSubject.setParse(parse);
                        optionSubject.setIsDelete(false);
                        optionSubject.setModifyTime(new Date());
                        optionSubject.setCreator("10000000");
                        optionSubject.setCreateTime(new Date());
                        optionSubject.setCreatorName("admin");
                        optionSubject.setQuestionId(questionId);
                        optionSubject.setType(SubjectTypeEnum.JUDGE.getValue());
                        QuestionSubject result = save(optionSubject);

                        QuestionSubjectAnswer questionSubjectAnswer = new QuestionSubjectAnswer();
                        questionSubjectAnswer.setAnswer(answer);
                        questionSubjectAnswer.setSubjectId(result.getId());
                        questionSubjectAnswer.setCreator(result.getCreator());
                        questionSubjectAnswer.setCreateTime(result.getCreateTime());
                        questionSubjectAnswer.setCreatorName(result.getCreatorName());
                        questionSubjectAnswer.setParse(parse);
                        questionSubjectAnswerService.save(questionSubjectAnswer);

                    }

                });


            }
            if (excelSheetDataDTO.getSheetName().equals("填空题")) {

            }
            if (excelSheetDataDTO.getSheetName().equals("问答题")) {

            }

        });

        return excelSheetDataDTOList;
    }

    public int updateIsDelete(Boolean isDelete, List<Long> id) {
        return repository.updateIsDelete(isDelete, id);
    }

    public int updateQuestionId(Long questionId, List<Long> id) {
        return repository.updateQuestionId(questionId, id);
    }

    public List<QuestionSubjectExportVO> findQuestionSubjectExportVOByQuestionIdList(List<Long> questionIdList) {
        List<QuestionSubjectExportVO> questionSubjectExportVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(questionIdList)) {
            List<Map<String, Object>> mapList = repository.selectQuestionSubjectByQuestionIdList(questionIdList);
            mapList.stream().forEach(map -> {
                QuestionSubjectExportVO questionSubjectExportVO = JsonUtil.fromMap(map, QuestionSubjectExportVO.class);
                questionSubjectExportVOList.add(questionSubjectExportVO);
            });
        }


        return questionSubjectExportVOList;
    }

    public List<Map<String, Object>> findMapListByQuestionIdList(List<Long> questionIdList) {
        return repository.selectQuestionSubjectByQuestionIdList(questionIdList);
    }


    public HSSFWorkbook exportQuestionSubject(List<QuestionSubjectExportVO> questionSubjectExportVOList) {

        if (!CollectionUtils.isEmpty(questionSubjectExportVOList)) {
            questionSubjectExportVOList = questionSubjectExportVOList.stream().map(questionSubjectExportVO -> {
                if (SubjectTypeEnum.SINGLE_OPTION.getValue().equals(questionSubjectExportVO.getType()) || SubjectTypeEnum.MULTIPLE_OPTION.getValue().equals(questionSubjectExportVO.getType())) {
                    List<QuestionSubjectOption> questionSubjectOptionList = questionSubjectOptionService.findQuestionSubjectOptionBySubjectId(questionSubjectExportVO.getSubjectId());
                    questionSubjectExportVO.setQuestionSubjectOptionList(questionSubjectOptionList);
                }
                return questionSubjectExportVO;
            }).collect(Collectors.toList());
        }

        String singleNotice = "填写须知：\r\n" +
                "1、请勿更改下表的标题\r\n" +
                "2、多选题的选项至少2个(选项A～B)，至多8个(选项A～H)，选项列只需前2列有内容，后面的列可以留空，但切勿删除选项列\r\n" +
                "3、正确答案仅能在ABCDEFGH8个字母中填写，并且字母间不允许空格\r\n" +
                "4、答案解析如无请留空，切勿删除该列\r\n" +
                "5、题目至多500字符，选项至多100字符，答案解析至多2000字符\r\n" +
                "6、单选题、判断题、填空题和问答题请在底部切换到其他工作表页";
        String multpleNotice = "填写须知：\r\n" +
                "1、请勿更改下表的标题\r\n" +
                "2、多选题的选项至少2个(选项A～B)，至多8个(选项A～H)，选项列只需前2列有内容，后面的列可以留空，但切勿删除选项列\r\n" +
                "3、正确答案仅能在ABCDEFGH8个字母中填写，并且字母间不允许空格\r\n" +
                "4、答案解析如无请留空，切勿删除该列\r\n" +
                "5、题目至多500字符，选项至多100字符，答案解析至多2000字符\r\n" +
                "6、单选题、判断题、填空题和问答题请在底部切换到其他工作表页";
        String judgeNotice = "填写须知：\r\n" +
                "1、请勿更改下表的标题\r\n" +
                "2、正确答案仅能填写“正确”或者“错误”\r\n" +
                "3、答案解析如无请留空，切勿删除该列\r\n" +
                "4、题目至多500字符，答案解析至多2000字符\r\n" +
                "5、单选题、多选题、填空题和问答题请在底部切换到其他工作表页";
        List<String> optionFildList = Arrays.asList("题目", "答案解析", "正确答案", "选项A", "选项B", "选项C", "选项D", "选项E", "选项F", "选项G", "选项H");
        List<String> firstRowFildList = Arrays.asList("题目", "答案解析", "正确答案");
        List<String> fildPropertyList = Arrays.asList("subject", "parse", "answer");

        List<String> sheetNameList = Arrays.asList(SubjectTypeEnum.SINGLE_OPTION.getName(), SubjectTypeEnum.MULTIPLE_OPTION.getName(), SubjectTypeEnum.JUDGE.getName());
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();


        List<QuestionSubjectExportVO> singleList = questionSubjectExportVOList.stream().filter(m -> SubjectTypeEnum.SINGLE_OPTION.getValue().equals(m.getType())).collect(Collectors.toList());
        List<QuestionSubjectExportVO> mutipleList = questionSubjectExportVOList.stream().filter(m -> SubjectTypeEnum.MULTIPLE_OPTION.getValue().equals(m.getType())).collect(Collectors.toList());
        List<QuestionSubjectExportVO> judgeList = questionSubjectExportVOList.stream().filter(m -> SubjectTypeEnum.JUDGE.getValue().equals(m.getType())).collect(Collectors.toList());
        sheetNameList.stream().forEach(sheetName -> {
            List<Column> columnList = new CopyOnWriteArrayList<>();
            if (sheetName.equals(SubjectTypeEnum.SINGLE_OPTION.getName())) {

                optionFildList.stream().forEach(str -> {
                    Column column = new Column(str, str, optionFildList.indexOf(str));
                    columnList.add(column);
                });
                this.exportExcel(hssfWorkbook, singleNotice, sheetName, singleList, columnList);
            }
            if (sheetName.equals(SubjectTypeEnum.MULTIPLE_OPTION.getName())) {
                optionFildList.stream().forEach(str -> {
                    Column column = new Column(str, str, optionFildList.indexOf(str));
                    columnList.add(column);
                });
                this.exportExcel(hssfWorkbook, multpleNotice, sheetName, mutipleList, columnList);
            }
            if (sheetName.equals(SubjectTypeEnum.JUDGE.getName())) {
                firstRowFildList.stream().forEach(str -> {
                    Column column = new Column(str, str, firstRowFildList.indexOf(str));
                    columnList.add(column);
                });
                this.exportExcel(hssfWorkbook, judgeNotice, sheetName, judgeList, columnList);
            }


        });


        return hssfWorkbook;

    }



    /**
     * 用于多Sheet导出，只插入数据，不保存文件
     *
     * @param workbook
     * @param title    sheetName
     * @param dataList
     * @param columns
     */
    public void exportExcel(HSSFWorkbook workbook, String notice, String title, List<QuestionSubjectExportVO> dataList, List<Column> columns) {
        try {
            // 声明一个工作薄
            // 生成一个表格
            HSSFSheet sheet = workbook.createSheet(title);
            // 设置密码保护表格不可编辑
            // sheet.protectSheet("12345678");
            // 设置表格默认列宽度为15个字节
            sheet.setDefaultColumnWidth(20);
            // 生成一个样式
            HSSFCellStyle columnStyle = workbook.createCellStyle();
            // 生成一个字体
            HSSFFont columnFont = workbook.createFont();

            columnFont.setFontHeightInPoints((short) 11);
            columnFont.setBold(false);
            // 把字体应用到当前的样式 206,255,206
//            HSSFPalette customPalette = workbook.getCustomPalette();
//            HSSFColor hssfColor=customPalette.addColor((byte)206,(byte)255,(byte)206);
            columnStyle.setFont(columnFont);

            columnStyle.setFillBackgroundColor(IndexedColors.LIME.getIndex());
            columnStyle.setFillPattern(FillPatternType.NO_FILL);
            columnStyle.setAlignment(HorizontalAlignment.CENTER);
            columnStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            // 生成并设置另一个样式
            HSSFCellStyle dataStyle = workbook.createCellStyle();
            // 生成另一个字体
            HSSFFont dataFont = workbook.createFont();
            dataFont.setBold(false);
            // 把字体应用到当前的样式
            dataStyle.setFont(dataFont);


            HSSFFont font3 = workbook.createFont();
            // 声明一个画图的顶级管理器
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            // 产生表格填写须知
            // 生成一个样式
            HSSFCellStyle noticeStyle = workbook.createCellStyle();
            // 生成一个字体
            HSSFFont noticeFont = workbook.createFont();
            noticeFont.setFontHeightInPoints((short) 12);
            noticeFont.setBold(false);
            noticeFont.setColor(HSSFColor.HSSFColorPredefined.RED.getIndex());
            // 把字体应用到当前的样式
            noticeStyle.setFont(noticeFont);
            HSSFRow noticeRow = sheet.createRow(0);
            HSSFCell noticeCell = noticeRow.createCell(0);
            noticeStyle.setWrapText(true);
            noticeCell.setCellStyle(noticeStyle);
            HSSFRichTextString noticetext = new HSSFRichTextString(notice);
            noticeCell.setCellValue(noticetext);
            noticeRow.setHeightInPoints(110);
            // 合并单元格
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 10));
            // 产生表格标题行
            HSSFRow row = sheet.createRow(1);
            for (int i = 0; i < columns.size(); i++) {
                Column column = columns.get(i);
                if (column.isVisible()) {
                    sheet.setColumnWidth(i, 100 * column.getWidth());
                    HSSFCell cell = row.createCell(i);
                    cell.setCellStyle(columnStyle);
                    HSSFRichTextString text = new HSSFRichTextString(column.getTitle());
                    cell.setCellValue(text);
                }
            }

            if(!CollectionUtils.isEmpty(dataList)) {
                dataList.stream().forEach(questionSubjectExportVO->{
                    int index=dataList.indexOf(questionSubjectExportVO);
                    HSSFRow  dataRow = sheet.createRow(index+2);
                    List<QuestionSubjectOption> questionSubjectOptionList = questionSubjectExportVO.getQuestionSubjectOptionList();
                    for (int j = 0; j < columns.size(); j++) {
                        Column column = columns.get(j);
                        if (column.isVisible()) {
                            sheet.setColumnWidth(j, 100 * column.getWidth());
                            HSSFCell cell = dataRow.createCell(j);
                            cell.setCellStyle(columnStyle);
                            HSSFRichTextString text = null;
                            if (j == 0) {
                                text = new HSSFRichTextString(questionSubjectExportVO.getSubject());
                            } else if (j == 1) {
                                text = new HSSFRichTextString(questionSubjectExportVO.getParse());
                            } else if (j == 2) {
                                text = new HSSFRichTextString(questionSubjectExportVO.getAnswer());
                            } else {
                                if(j-3<questionSubjectOptionList.size()) {
                                    text = new HSSFRichTextString(questionSubjectOptionList.get(j - 3).getContent());
                                }
                            }
                            cell.setCellValue(text);
                        }
                    }

                });
            }
        } catch (Exception e) {
//           e.printStackTrace();
        }
    }


}
